import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../views/Login.vue";
import Home from "../views/Home.vue";
import children from "./children";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login,
      meta: { title: "登录" },
    },
    {
      path: "/",
      name: "home",
      component: Home,
      redirect: { path: children[0].path },
      meta: { title: "主页" },
      children,
    },
  ],
});

router.beforeEach((to) => {
  document.title = to.meta.title || "标题";
  const token = localStorage.getItem("token");
  if (!token && to.name !== "login") {
    return { name: "login" };
  }
});

export default router;
